package com.sjc.counter.v3;

import com.sjc.counter.entity.RequestInfo;
import com.sjc.counter.entity.RequestStat;
import com.sjc.counter.v1.MetricsStorage;
import com.sjc.counter.v1.RedisMetricsStorage;
import com.sjc.counter.v2.Aggregator;
import com.sjc.counter.v2.EmailViewer;
import com.sjc.counter.v2.StatViewer;

import java.util.*;

/**
 * @Classname EmailReporter
 * @Description
 * @Date 2020/5/1 18:22
 * @Created by SangJiacun
 */
public class EmailReporter extends ScheduledReporter{
    private static final Long DAY_HOURS_IN_SECONDS = 86400L;

    public EmailReporter(MetricsStorage metricsStorage, Aggregator aggregator, StatViewer viewer) {
        super(metricsStorage, aggregator, viewer);
    }

    // 兼顾代码的易用性，新增一个封装了默认依赖的构造函数
    public EmailReporter(List<String> emailToAddresses) {
        this(new RedisMetricsStorage(), new Aggregator(), new EmailViewer(emailToAddresses));
    }

    public void startDailyReport(){
        Date firstTime = trimTimeFieldsToZeroOfNextDay(new Date());
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                long durationInMillis = DAY_HOURS_IN_SECONDS * 1000;
                long endTimeInMillis = System.currentTimeMillis();
                long startTimeInMillis = endTimeInMillis - durationInMillis;

                doStatAndReport(startTimeInMillis, endTimeInMillis);

            }
        }, firstTime, DAY_HOURS_IN_SECONDS * 1000);
    }

    // 设置成protected而非private是为了方便写单元测试
//    @VisibleForTesting
    protected Date trimTimeFieldsToZeroOfNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}
